package com.qianfeng.missfood.adapter;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qianfeng.missfood.R;
import com.qianfeng.missfood.category_bean.Category_Detail_Bean;
import com.qianfeng.missfood.fragment.Category_Detail_ptrListView_Fragment;
import com.qianfeng.missfood.ui.CategoryDetailsActivity;

import java.util.List;

/**
 * Created by Administrator on 2015/8/19.
 */
public class Category_xiangqing_RecyclerView_Adapter extends RecyclerView.Adapter<Category_xiangqing_RecyclerView_Adapter.MyViewHolder> {
    private List<String> list;
    private int index;
    private FragmentManager fragmentManager;
    private List<String> categoryIds;
    private TextView level3_name;
    public int sortType;

    public Category_xiangqing_RecyclerView_Adapter(List<String> list, int position, FragmentManager fragmentManager, List<String> categoryIds, TextView level3_name) {
        this.list = list;
        this.index = position;
        this.fragmentManager = fragmentManager;
        this.categoryIds = categoryIds;
        this.level3_name = level3_name;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.category_details_recyclerviewitem_layout, parent, false);
        MyViewHolder myViewHolder = new MyViewHolder(itemView);
        return myViewHolder;


    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        holder.textView.setText(list.get(position));
        if (position == index) {
            holder.textView.setBackgroundResource(R.drawable.category_check_back_shape);
        } else {
            holder.textView.setBackgroundResource(R.drawable.category_check_unback_shape);
        }


    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {
        private TextView textView;

        public MyViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.category_listview_item);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //给itemview设置点击事件   更换fragment
                    //给CategoryDetailsActivity传递现在的位置
                    CategoryDetailsActivity.adapterPosition=getPosition();

                    //把level3的内容更改
                    level3_name.setText(list.get(getPosition()));
                    Bundle bundle = new Bundle();
                    bundle.putString("categoryId", categoryIds.get(getPosition()));
                    bundle.putInt("sortType", sortType);
                    Category_Detail_ptrListView_Fragment detail_ptrListView_fragment = new Category_Detail_ptrListView_Fragment();
                    detail_ptrListView_fragment.setArguments(bundle);
                    fragmentManager.beginTransaction().replace(R.id.details_ptrListView_container, detail_ptrListView_fragment).commit();
                    //更换textview的背景:直接更换index,然后刷新下adapter就行了
                    index=getPosition();
                    Category_xiangqing_RecyclerView_Adapter.this.notifyDataSetChanged();
                }
            });
        }
    }
}
